package serverside;

import javax.swing.*;

import java.awt.Toolkit;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import static javax.swing.SwingConstants.CENTER;

public class CheckATMs extends JPanel {
    ArrayList<JButton> buttons = new ArrayList<JButton>();
    ArrayList<JLabel> lamps = new ArrayList<JLabel>();
    private JButton back = new JButton("Back");
    private JButton atmButton;
    private JLabel lamp;
    Font font = new Font("Arial", Font.BOLD, 12);
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    ArrayList<Boolean> on = new ArrayList<Boolean>();
    String[][]datas ;//= {{"gfd","fdgfd","dfgdf"},{"dfgdf","dfgdf","dfgfd"},{"dfgfd","dddf","jhd"}};
    int index = 0, b = 0, j = 0, k = 0, leftbutton = 0, leftlamp = 240, morethan4 = 0;

    CheckATMs() {
        dim.width = 900;
        dim.height = 400;
        setOpaque(false);
        setPreferredSize(dim);
        setEnabled(true);
        setLayout(null);
        /* for(int i = 0 ; i < ind ; i++)
         {
         if(i > 3)
         {
         leftbutton = 330;
         leftlamp = 570;
         morethan4 = 4;
         }
         else
         {
         leftbutton = 0;
         leftlamp = 240;
         morethan4 = 0;
         }
            
         on.add(false);
            
         //add(atmButton);
         lamp = new JLabel("Offline" , CENTER);
         lamps.add(i , lamp);
         lamps.get(i).setBounds(leftlamp,((i - morethan4)*60) + 0 , 40 , 40);
         lamps.get(i).setEnabled(true);
         lamps.get(i).setOpaque(true);
         lamps.get(i).setBackground(Color.red);
         lamps.get(i).setForeground(Color.blue);
         lamps.get(i).setFont(font);
            
         //add(lamp);
            
         atmButton = new JButton(Integer.toString(i));
         buttons.add(i , atmButton);
         buttons.get(i).setBounds(leftbutton,((i - morethan4)*60) + 0 , 200 , 40);
         buttons.get(i).setEnabled(true);
         buttons.get(i).setFont(font);
         buttons.get(i).setForeground(Color.blue);
         buttons.get(i).addMouseListener(new MouseAdapter(){
         public void mousePressed(MouseEvent e) {
         if(on[i] == false){
         lamps.get(i).setBackground(Color.green);
         lamps.get(i).setText("Online");
         on[i] = true;
         }
         else{
         lamps.get(i).setBackground(Color.red);
         lamps.get(i).setText("Offline");
         on[i] = false;
         }
         }
         });
            
         add(lamps.get(i));
         add(buttons.get(i));
            
         }
        
         for(j = 0 ; j < ind ; j++){
         k = j;
         buttons.get(j).addMouseListener(new MouseAdapter(){
         int x = k;
         public void mousePressed(MouseEvent e) {
         if(on.get(x) == false){
         lamps.get(x).setBackground(Color.green);
         lamps.get(x).setText("Online");
         on.set(x, true);
         }
         else{
         lamps.get(x).setBackground(Color.red);
         lamps.get(x).setText("Offline");
         on.set(x, false);
         }
         }
         });
         }
         //j = 0;
         ind++;*/
        back.setBounds(700, 360, 200, 40);
        back.setEnabled(true);
        back.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                b = 1;
            }
        });
        add(back);
    }

    //public void DisplayATMs(String ind, String city, String region) {
    public void DisplayATMs(int ind) {
        try {
            DatabasePack.ATM atm = new DatabasePack.ATM();
            //JOptionPane.showMessageDialog(null, "does XD");
            datas = atm.ATMs();
            index = ind;
            //JOptionPane.showMessageDialog(null, lamps.size());
            //for (int j = 0; j < ind; j++) {
            //for (int i = 0; i < datas.length; i++) {
            //int i = ind - 1;
            int i = index;
            if (i > 3) {
                leftbutton = 330;
                //leftlamp = 570;
                leftlamp = 330;
                morethan4 = 4;
            } else {
                leftbutton = 0;
                //leftlamp = 240;
                leftlamp = 0;
                morethan4 = 0;
            }
//            if(i % 2 != 0)
                on.add(false);
//            else
//                on.add(true);

            //add(atmButton);
            lamp = new JLabel(datas[i][0] + " - " + datas[i][1] + " - " + datas[i][2], CENTER);
            lamps.add(i, lamp);
            lamps.get(i).setBounds(leftlamp, ((i - morethan4) * 60) + 0, 230, 40);
            lamps.get(i).setEnabled(true);
            lamps.get(i).setOpaque(true);
            lamps.get(i).setBackground(Color.red);
            lamps.get(i).setForeground(Color.blue);
            lamps.get(i).setFont(font);

                //add(lamp);
            /*
             atmButton = new JButton(name);
             buttons.add(i , atmButton);
             buttons.get(i).setBounds(leftbutton,((i - morethan4)*60) + 0 , 200 , 40);
             buttons.get(i).setEnabled(true);
             buttons.get(i).setFont(font);
             buttons.get(i).setForeground(Color.blue);*//*
             buttons.get(i).addMouseListener(new MouseAdapter(){
             public void mousePressed(MouseEvent e) {
             if(on[i] == false){
             lamps.get(i).setBackground(Color.green);
             lamps.get(i).setText("Online");
             on[i] = true;
             }
             else{
             lamps.get(i).setBackground(Color.red);
             lamps.get(i).setText("Offline");
             on[i] = false;
             }
             }
             });*/

            add(lamps.get(i));
                //add(buttons.get(i));

            //}
            // }
        /*
             for(j = ind - 1 ; j < ind ; j++){
             k = j;
             buttons.get(j).addMouseListener(new MouseAdapter(){
             int x = k;
             public void mousePressed(MouseEvent e) {
             if(on.get(x) == false){
             lamps.get(x).setBackground(Color.green);
             lamps.get(x).setText("Online");
             on.set(x, true);
             }
             else{
             lamps.get(x).setBackground(Color.red);
             lamps.get(x).setText("Offline");
             on.set(x, false);
             }
             }
             });
             }*/
            //j = 0;
            index++;
        } 
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Sorry , can't display the ATM ");
        }
    }
    void Increment(){
        index++;
    }
}
